package com.mdd.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdd.product.entity.ProductType;
import com.mdd.product.mapper.ProductTypeMapper;
import com.mdd.product.service.IProductTypeService;
import com.mdd.product.validate.ProductTypeCreateValidate;
import com.mdd.product.validate.ProductTypeUpdateValidate;
import com.mdd.product.vo.ProductTypeListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 商品类别实现类
 * @author LikeAdmin
 */
@Service
public class ProductTypeServiceImpl implements IProductTypeService {

    @Resource
    ProductTypeMapper productTypeMapper;

    /**
     * 商品类别列表
     *
     * @author LikeAdmin
     * @return PageResult<ProductTypeListedVo>
     */
    @Override
    public List<ProductTypeListedVo> list() {
        LambdaQueryWrapper<ProductType> productType=new LambdaQueryWrapper<>();
        List<ProductType> data = productTypeMapper.selectList(productType);
        return recursionNextRoute(0L,data);
    }
    private List<ProductTypeListedVo> recursionNextRoute(Long parentId, List<ProductType> menuList) {
        List<ProductTypeListedVo> list = new ArrayList<>();
        Optional.ofNullable(menuList).ifPresent(menus -> menus.stream()
                .filter(menu -> parentId.equals(menu.getPId()))
                .forEach(menu -> {
                    ProductTypeListedVo vo = new ProductTypeListedVo();
                    BeanUtils.copyProperties(menu, vo);
                    // 递归获取子节点
                    List<ProductTypeListedVo> children = recursionNextRoute(menu.getId(), menuList);
                    vo.setChildren(children);
                    list.add(vo);
                }));
        return list;
    }
    /**
     * 商品类别新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(ProductTypeCreateValidate createValidate) {
        ProductType model = new ProductType();
        model.setPId(createValidate.getPId());
        model.setName(createValidate.getName());
        productTypeMapper.insert(model);
    }

    /**
     * 商品类别编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(ProductTypeUpdateValidate updateValidate) {
        ProductType model = productTypeMapper.selectById(updateValidate.getId());

        Assert.notNull(model, "商品类别数据不存在!");

        model.setName(updateValidate.getName());
        productTypeMapper.updateById(model);
    }

    /**
     * 商品类别删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        ProductType model = productTypeMapper.selectById(id);

        Assert.notNull(model, "数据不存在!");

        productTypeMapper.deleteById(id);
    }

}
